#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const int MaxNum = 100;
const string password = "abcd";
int count = 0;
string name[MaxNum];
string sex[MaxNum];
string branch[MaxNum];
string type[MaxNum];
string birthday[MaxNum];

int passwordValid();
void menu();
void append();
void search();
void show();

int main() {
    if (passwordValid())
        menu();
    return 0;
}

int passwordValid() {
    string ps;
    int n = 0;
    cout << "请输入登录密码：" << endl;
    while (n < 3) {
        cin >> ps;
        if (password == ps) {
            cout << "密码正确!" << endl;
            return 1;
        } else {
            cout << "输入密码错误!" << endl;
            n++;
            if (n == 3) {
                cout << "密码已输入3次，您无权操作!\n";
                return 0;
            }
        }
    }
    return 0;
}

void menu() {
    char c;
    do {
        for (int i = 0; i < 60; i++)
            cout << "*";
        cout << endl;
        cout << "\t1: 添加党员信息\n\t2: 查找党员信息\n";
        cout << "\t3: 显示党员信息\n\t4: 退出系统\n";
        cout << "\t请输入选项[1~4]:\n";

        cin >> c;

        switch (c) {
        case '1':
            append();
            break;
        case '2':
            search();
            break;
        case '3':
            show();
            break;
        case '4':
            exit(0);
        default:
            cout << "无效选项，请重新输入。\n";
        }
        
        cout << "按Enter键返回主菜单：\n";
        cin.ignore(); // 清除缓冲区
        cin.get(); // 等待用户按下Enter键
    } while (true);
}

void append() {
    int n;
    cout << "请输入要添加的党员信息人数：";
    cin >> n;
    cin.ignore(); // 清除缓冲区

    cout << "请输入党员基本信息：\n";
    for (int i = 0; i < n; i++) {
        cout << "党员姓名: ";
        getline(cin, name[count]);
        cout << "党员性别(男/女): ";
        getline(cin, sex[count]);
        cout << "支部信息: ";
        getline(cin, branch[count]);
        cout << "党员类型(预备党员/党员): ";
        getline(cin, type[count]);
        cout << "出生日期(年/月/日): ";
        getline(cin, birthday[count]);

        count++;
        if (count >= MaxNum) {
            cout << "已达到最大存储数量！\n";
            break;
        }
    }
}

void search() {
    string searchName;
    cout << "请输入要查找的党员姓名: ";
    cin >> searchName;
    
    bool found = false;
    for (int i = 0; i < count; i++) {
        if (searchName == name[i]) {
            cout << "党员姓名: " << setw(14) << name[i] << endl;
            cout << "性别: " << setw(14) << sex[i] << endl;
            cout << "支部信息: " << setw(14) << branch[i] << endl;
            cout << "党员类型: " << setw(14) << type[i] << endl;
            cout << "出生日期: " << setw(14) << birthday[i] << endl;
            found = true;
            break;
        }
    }
    if (!found)
        cout << "抱歉!没有找到此党员的信息!\n";
}

void show() {
    if (count == 0) {
        cout << "暂无党员信息。\n";
        return;
    }
    
    cout << setw(14) << "党员姓名" 
         << setw(14) << "性别"
         << setw(14) << "支部信息"
         << setw(14) << "党员类型"
         << setw(14) << "出生日期" 
         << endl;

    for (int i = 0; i < count; i++) {
        cout << setw(14) << name[i]
             << setw(14) << sex[i]
             << setw(14) << branch[i]
             << setw(14) << type[i]
             << setw(14) << birthday[i]
             << endl;
    }
}
